package com.zt.rongda.stock.service;

import com.zt.rongda.stock.inter.PickMaterialMapper;
import com.zt.rongda.stock.model.PickMaterial;
import com.zt.rongda.constant.BusConstant;
import com.zt.system.model.BusinessException;
import com.zt.servlet.ReqBean;
import com.zt.system.model.SqlBuilder;
import com.zt.system.service.BaseService;
import com.zt.util.StringUtil;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class PickMaterialService extends BaseService {

    @Autowired
    private PickMaterialMapper pickMaterialMapper;

    /**
    * 查询
    *
    * @param reqBean
    * @return
    */
    public JSONObject getTable(ReqBean reqBean) {
        SqlBuilder sqlBuilder = new SqlBuilder("");
        sqlBuilder.appendSql("select m.*,d.DEPT_NAME from PICK_MATERIALS m ");
        sqlBuilder.appendSql("LEFT JOIN SYS_DEPARTMENT d ON d.DEPT_ID = m.PICK_DEPT_ID ");
        sqlBuilder.addWhereLike2("m.STOCK_ID", reqBean.getInfo("stockId"));
        sqlBuilder.addWhereLike2("m.STOCK_NAME",reqBean.getInfo("stockName"));
        return queryTable(sqlBuilder, reqBean.getPageSize(), reqBean.getPageIndex());
    }

    /**
    * 通过id查询
    *
    * @param billId
    * @return
    */
    public PickMaterial getById(String billId) {
        return pickMaterialMapper.selectByPrimaryKey(billId);
    }

    /**
    * 新增
    *
    * @param pickMaterial
    */
    public void add(PickMaterial pickMaterial) {
        pickMaterial.setBillId(intIncrementId("PICK_MATERIALS","BILL_ID"));// 手动设置Id

        pickMaterial.setStatus(BusConstant.PickMaterial.STATUS_NOT_PICKING);
        pickMaterial.setMakeTime(new Date());
        pickMaterial.setModifyTime(pickMaterial.getMakeTime());
        pickMaterialMapper.insert(pickMaterial);
    }

    /**
    * 修改
    *
    * @param pickMaterial
    */
    public void update(PickMaterial pickMaterial) {
        String billId = pickMaterial.getBillId();
        if (StringUtil.isEmpty(billId)) {
            throw new BusinessException("获取主键失败");
        }

        pickMaterial.setModifyTime(new Date());
        pickMaterialMapper.updateByPrimaryKeySelective(pickMaterial);
    }

    /**
    * 删除
    *
    * @param billId
    */
    public void delete(String billId) {
        if (StringUtil.isEmpty(billId)) {
            throw new BusinessException("获取主键失败");
        }
        pickMaterialMapper.deleteByPrimaryKey(billId);
    }

}